﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Ink;

namespace MBO_PIM
{
    class GestenFrame
    {
        private double start_left;
        private double start_top;
        private double gesture_width;
        private double gesture_height;
        
        private static int MIN_WIDTH = 30;
        private static int MIN_HEIGHT = 30;
      
        private double MARGIN_ROW = 0;
        private double MARGIN_COLUMN = 0;

        private double fieldWidth = MIN_WIDTH;
        private double fieldHeight = MIN_HEIGHT;

        private int lastField = 0;
        private Stroke stroke;

        public string berechneteGeste = "";


        public GestenFrame(Stroke stroke)
        {
            this.stroke = stroke;

            this.gesture_height = this.stroke.GetBounds().Height;
            this.gesture_width = this.stroke.GetBounds().Width;

            this.start_left = this.stroke.GetBounds().Left;
            this.start_top = this.stroke.GetBounds().Top;

            double w = (gesture_width) / 2;
            if (w > fieldWidth)
                fieldWidth = w;
            double h = (gesture_height) / 2;
            if (h > fieldHeight)
                fieldHeight = h;

            MARGIN_COLUMN = fieldWidth * 0.2;
            MARGIN_ROW = fieldHeight * 0.2;


            for (int i = 0; i < this.stroke.StylusPoints.Count; i++)
            {
                int currentField = this.getField(this.stroke.StylusPoints[i].X, this.stroke.StylusPoints[i].Y);
                if (currentField==0) continue;
                if (lastField == 0)
                {
                    lastField = currentField;
                    continue;
                }
                else if (lastField == currentField)
                    continue;

                if (lastField == 3 && currentField == 1 || lastField == 4 && currentField == 2)
                    this.berechneteGeste += "_N";
                else if (lastField == 1 && currentField == 3 || lastField == 2 && currentField == 4)
                    this.berechneteGeste += "_S";
                else if (lastField == 2 && currentField == 1 || lastField == 4 && currentField == 3)
                    this.berechneteGeste += "_W";
                else if (lastField == 1 && currentField == 2 || lastField == 3 && currentField == 4)
                    this.berechneteGeste += "_E";
                else if (lastField == 3 && currentField == 2)
                    this.berechneteGeste += "_NE";
                else if (lastField == 4 && currentField == 1)
                    this.berechneteGeste += "_NW";
                else if (lastField == 1 && currentField == 4)
                    this.berechneteGeste += "_SE";
                else if (lastField == 2 && currentField == 3)
                    this.berechneteGeste += "_SW";

                lastField = currentField;
            }
        }

        private int getField(double x, double y) {
            x -= this.start_left;
            y -= this.start_top;
 
            if (y < fieldHeight) {
                if (x < fieldWidth)
                    return 1;
                else if (x > fieldWidth + MARGIN_COLUMN)
                    return 2;
            }
       
            else if (y > fieldHeight + MARGIN_ROW)
            {
                if (x < fieldWidth)
                    return 3;
                else if (x > fieldWidth + MARGIN_COLUMN)
                    return 4;
            }
            return 0;
        }
    }
}
